/*	kd:
	This is a measly shop container that can also help you cycle through shops,
	as well as save preferences and such. If zs had struct assignment, this
	would not be a class. */

class Gcs_ShopPack {
	protected bool is_init;
	
	virtual void Init () {
		if(is_init) {
			return;
		}
		
		// Initialise the shop instances here.
		
		is_init = true;
	}
}

class Gcs_ShopList {
	private Array <Gc_Shop_Inv>    shop_inst;
	
	int ShopCount () {
		return shop_inst.size();
	}
	
	const err_tried_adding_shop_twice = 0x7fffffff;
	
	int Push (Gc_Shop_Inv shop) {
		if(shop == NULL) {
			return -1;
		}
		
		// Don't add the same shop twice:
		int size = shop_inst.Size();
		int i = shop_inst.Find(shop);
		
		if(0 < size && i != size) {
			return -1;
		}
		
		shop_inst.Push(shop);
		return size;
	}
	
	void CopyWithoutHiddenItems (Gcs_ShopList source) {
		shop_inst.clear();
		
		for(let i = 0; i < source.ShopCount(); i++) {
			let source_inst = source.shop_inst [i];
			
			if(source_inst.IsAllSecrets() || source_inst.IsEmpty()) {
				continue;
			}
			
			let temp_inst = new("Gc_Shop_Inv");
			
			if(temp_inst) {
				shop_inst.push(temp_inst);
				temp_inst.CopyWithoutHiddenItems(source_inst);
			}
		}
	}
	
	Gc_Shop_Inv At (uint handle) const {
		if(handle < shop_inst.Size()) {
			return shop_inst [handle];
		}
		
		return NULL;
	}
	
	// kd:
	// Help cycling through the shops. Returns new handle, instance to shop
	// and whether the shop changed.
	
	uint, Gc_Shop_Inv NextShop (uint current) const {
		uint size = shop_inst.Size();
		
		if(1 < size) {
			current++;
			
			if(size <= current) {
				current -= size;
			}
			
			return current, shop_inst [current];//, true;
		}
		
		return current, shop_inst [current];//, false;
	}
	
	uint, Gc_Shop_Inv PrevShop (uint current) const {
		uint size = shop_inst.Size();
		
		if(1 < size) {
			current--;
			
			if(current < 0) {
				current += size;
			}
			
			return current, shop_inst [current];//, true;
		}
		
		return current, shop_inst [current];//, false;
	}
	
	// kd: Find an item in any shop we have.
	Gc_Shop_Inv, int FindInventory (class <Inventory> inv_class) const {
		for(let i = 0; i < shop_inst.size(); i++) {
			let item = shop_inst [i].FindItem(inv_class);
			
			if(0 <= item) {
				return shop_inst [i], item;
			}
		}
		
		return NULL, -1;
	}
}

/*	kd:
	A shop's data is contained here, as well as a bunch of functions to interact
	with it, so stuff can be bought. The idea is to create events that higher
	level code can interact with, depending on the interaction results.
	
	Of course, you want it in higher level code because that's where all the
	objects are going to be. You don't really want to pass everything into
	this class here.
	
	These events are purely for sorting code. We're never going to pass more
	data than "oh it worked", so an enum is okay.
	
	*/

class Gc_Shop_Inv {
	const currency = "Dosh";
	
	// kd: All the items inside.
	protected Array <class <Inventory> >	inv_class;
	protected Array <String>				resource_key;
	protected Array <Gcs_ItemType>			type;
	protected Array <int>					cost;
	protected Array <int>					amount;
	protected Array <bool>					is_hidden;
	protected Array <bool>					is_golden;
	protected Array <class <Gc_ScreenParticleGen> >	particle_gen;
	protected String						name_lang;
	protected int							count;
	
	void SetNameLang (String name_lang_) {
		name_lang = name_lang_;
	}
	
	String ShopName () const {
		return StringTable.Localize(name_lang, false);
	}
	
	void CopyWithoutHiddenItems (Gc_Shop_Inv source) {
		inv_class.		copy(source.inv_class);
		resource_key.	copy(source.resource_key);
		type.			copy(source.type);
		cost.			copy(source.cost);
		amount.			copy(source.amount);
		is_golden.		copy(source.is_golden);
		is_hidden.		copy(source.is_hidden);
		particle_gen.	copy(source.particle_gen);
		name_lang = source.name_lang;
		
		for(let i = source.count; 0 <= --i;) {
			if(is_hidden [i]) {
				inv_class.		delete(i);
				resource_key.	delete(i);
				type.			delete(i);
				cost.			delete(i);
				amount.			delete(i);
				is_golden.		delete(i);
				is_hidden.		delete(i);
				particle_gen.	delete(i);
			}
		}
		
		count = inv_class.size();
	}
	
	// kd: Let's fill that list in.
	int AddForSale (
	class <Inventory>	inv_class_,
	Gcs_ItemType		type_,
	int					price_,
	int					amount_					= 1,
	bool				is_secret				= false,
	string				resource_key_			= "",
	class <Gc_ScreenParticleGen>	particle_fx	= NULL) {
		if(type_ == NULL || inv_class_ == NULL) {
			return 0;
		}
		
		if(resource_key_.length() < 1) {
			resource_key_ = inv_class_.GetClassName();
		}
		
		let i = inv_class.find(inv_class_);
		
		if(i != inv_class.size()) {
			return 0;
		}
		
		inv_class.		push(inv_class_);
		resource_key.	push(resource_key_);
		type.			push(type_);
		cost.			push(max(price_, 0));
		amount.			push(max(amount_, 1));
		is_golden.		push(is_secret);
		is_hidden.		push(is_secret);
		particle_gen.	push(particle_fx);
		count			= inv_class.size();
		return count - 1;
	}
	
	// kd: You can use this to prevent checking if the thing is there when
	// zs is compiling. I use this to make a weapons shop that doesn't know if
	// the addon is loaded.
	int AddSoft (
	String				inv_str,
	Gcs_ItemType		type_,
	int					price_,
	int					amount_					= 1,
	bool				is_secret				= false,
	string				resource_key_			= "",
	class <Gc_ScreenParticleGen>	particle_fx	= NULL) {
		return AddForSale(
			inv_str,
			type_,
			price_,
			amount_,
			is_secret,
			resource_key_,
			particle_fx);
	}
	
	void HideAll () {
		for(let i = 0; i < count; i++) {
			is_hidden [i] = true;
		}
	}
	
	void UnlockClass (class <Inventory> inv) {
		let i = inv_class.find(inv);
		
		if(i != count) {
			is_hidden [i] = false;
		}
	}
	
	void Unlock (uint item_i) const {
		if(item_i < count) {
			is_hidden [item_i] = false;
		}
	}
	
	class <Inventory> At (uint item_i) const {
		return inv_class [item_i];
	}
	
	class <Inventory> InvClass (uint item_i) const {
		return inv_class [item_i];
	}
	
	Gcs_ItemType Handler (uint item_i) const {
		return type [item_i];
	}
	
	int Price (uint item_i) const {
		return cost [item_i];
	}
	
	int GiveAmount (uint item_i) const {
		return amount [item_i];
	}
	
	bool IsGolden (uint item_i) const {
		return is_golden [item_i];
	}
	
	bool IsHidden (uint item_i) const {
		return is_hidden [item_i];
	}
	
	class <Gc_ScreenParticleGen> ParticleGen (uint item_i) const {
		return particle_gen [item_i];
	}
	
	String ResourceKey (uint item_i) const {
		return resource_key [item_i];
	}
	
	int ItemCount () const {
		return count;
	}
	
	bool IsAllSecrets () const {
		for(let i = 0; i < count; i++)
		if(!is_hidden [i]) {
			return false;
		}
		
		return true;
	}
	
	bool IsEmpty () const {
		return count < 1;
	}
	
	void ClearItems () {
		inv_class.		clear();
		resource_key.	clear();
		type.			clear();
		cost.			clear();
		amount.			clear();
		is_hidden.		clear();
		count = 0;
	}
	
	int FindItem (class <Inventory> inv) {
		let i		= inv_class.find(inv);
		let size	= inv_class.size();
		
		if(0 < size && i != size) {
			return i;
		}
		
		return -1;
	}
	
	// Conveniently, get the item description by a certain shopkeeper.
	
	String ItemDescription (uint item_handle, String desc_key) const {
		if(item_handle < count) {
			String default_msg =
				type [item_handle].DefaultDesc(inv_class [item_handle]);
			
			// kd: If the key has no message attached, we'll fall back to
			// Nithor's by default.
			
			let key		= String.Format(
				"%s_desc_%s",
				desc_key,
				resource_key [item_handle]);
			let loc_str	= StringTable.Localize(key, false);
			
			if(loc_str ~== key) {
				key = String.Format(
					"nithor_desc_%s",
					resource_key [item_handle]);
				loc_str	= StringTable.Localize(key, false);
			}
			
			// Here's something funny. The message scroll uses BrokenLines.
			// gzd thinks it's being helpful by throwing out trailing new
			// lines. That's what we don't want, so we add in some signs you
			// typically don't see in a font in order to force it.
			
			if(default_msg.Length() < 1) {
				return String.Format(
					"%s\n♪♪♪",
					loc_str);
			}
			
			else {
				return String.Format(
					"%s\n♪♪♪\n%s\n♪♪♪",
					loc_str,
					default_msg);
			}
		}
		
		return StringTable.Localize("no_description");
	}
	
	// kd: Title is always the same.
	
	String ItemTitle (uint item_handle) {
		return StringTable.Localize(String.Format(
			"$%s_title",
			resource_key [item_handle]) );
	}
	
	// kd: And this is the sndinfo key for the activation sound.
	
	Sound ActivationSound (uint item_handle) {
		return String.Format("%s_active", resource_key [item_handle]);
	}
	
	Sound DeactivationSound (uint item_handle) {
		return String.Format("%s_inactive", resource_key [item_handle]);
	}
	
	// kd: Tip handler class type.
	
	class <Gc_Shop_Tip> Tip (uint item_handle) {
		return type [item_handle].Tip();
	}
	
	// kd: Returns how many times the item can be bought.
	
	uint CanBuyXtimes (Actor user, uint item_handle) const {
		if(user && item_handle < count) {
			let account	= user.CountInv(currency);
			let price	= cost [item_handle];
			let diff	= account - price;
			
			if(0 <= diff) {
				return 1 + diff / price;
			}
		}
		
		return 0;
	}
	
	enum shop_event {
		EVENT_NONE = 0,
		EVENT_BUY_FREEBIE,
		EVENT_BUY_OKAY,
		EVENT_BUY_POOR,
		EVENT_BUY_MAXED,
		EVENT_BUY_WORSE,
		EVENT_BUY_OKAY_BUT_WORSE,
		EVENT_BUY_CANT_CARRY,
		EVENT_EQUIP_OKAY_ON,
		EVENT_EQUIP_OKAY_OFF,
		EVENT_EQUIP_DONT_HAVE,
		EVENT_EQUIP_CANT,
		EVENT_EQUIP_ALREADY,
		EVENT_EQUIP_NOT_YET,
		EVENT_BAD_HANDLER,
		EVENT_BAD_CHOICE,
		EVENT_NO_USER
	}
	
	shop_event BuyItem (Actor user, uint i) {
		if(!user) {
			return EVENT_NO_USER;
		}
		
		if(count <= i) {
			return EVENT_BAD_CHOICE;
		}
		
		if(!type [i]) {
			return EVENT_BAD_HANDLER;
		}
		
		if(type [i].MaxedOut(user, inv_class [i])) {
			return EVENT_BUY_MAXED;
		}
		
		if(!type [i].CanCarry(user, inv_class [i])) {
			return EVENT_BUY_CANT_CARRY;
		}
		
		if(user.CountInv(currency) < cost [i] && !sv_infiniteinventory) {
			return EVENT_BUY_POOR;
		}
		
		return EVENT_BUY_OKAY;
	}
	
	shop_event EquipItem (Actor user, uint i) {
		if(!user) {
			return EVENT_NO_USER;
		}
		
		if(count <= i) {
			return EVENT_BAD_CHOICE;
		}
		
		if(!type [i]) {
			return EVENT_BAD_HANDLER;
		}
		
		if(!type [i].Equippable(user, inv_class [i])) {
			return EVENT_EQUIP_CANT;
		}
		
		if(!type [i].Count(user, inv_class [i])) {
			return EVENT_EQUIP_DONT_HAVE;
		}
		
		if(!type [i].CanEquip(user, inv_class [i])) {
			return EVENT_EQUIP_NOT_YET;
		}
		
		if(type [i].CannotEquip(user, inv_class [i])) {
			return EVENT_EQUIP_ALREADY;
		}
		
		return type [i].IsEquipped(user, inv_class [i]) ?
			EVENT_EQUIP_OKAY_OFF : EVENT_EQUIP_OKAY_ON;
	}
}